//
//  ZHMenuItem.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/28.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation


enum ZHMenuState {
    case on
    case off
    case mix
}
enum ZHMenuType {
    case `default`
    case multiple
}



class ZHMenuItem: NSMenuItem {
    
    var saction:OneActionBlock?
    var size:NSSize = NSSize.zero
    
    override init(title string: String, action selector: Selector?, keyEquivalent charCode: String) {
        super.init(title: string, action: selector, keyEquivalent: charCode)
    }
    
    convenience init(title string: String,
                     keyEquivalent charCode: String,
                     saction:OneActionBlock?,
                     type:ZHMenuType = .default,
                     size:NSSize = NSSize.init(width: 260, height: 22)) {
        self.init()
        self.title = string
        self.string = string
        self.saction = saction
        self.size = size
        self.type = type
        self.setupUI()
        self.loadData()
    }
    
    var string: String = "" {
        didSet {
            self.sTitle.stringValue = string
        }
    }
    
    var kstate:ZHMenuState = .off {
        didSet {
            self.icon.isHidden = (kstate == .on) ? false:true
            let width = (kstate == .off) ? 16:16
            self.icon.snp.updateConstraints { (make) in
                make.width.equalTo(width)
            }
        }
    }
    
    var type:ZHMenuType = .default
    func setMenu(type:ZHMenuType) {
        var width = 0
        switch type {
        case .multiple:
            width = 15
        default:
            width = 0
        }
        self.icon.snp.updateConstraints { (make) in
            make.width.equalTo(width)
        }
    }
    
    
    func setupUI() {
        self.container.frame = NSRect.init(x: 0, y: 0, width: self.size.width, height: self.size.height)
        
        let w = self.type == .multiple ? 15:0
        self.container.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.width.equalTo(w)
            make.height.equalTo(15)
            make.centerY.equalToSuperview()
            make.left.equalTo(5)
        }
        self.container.addSubview(sTitle)
        sTitle.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.centerY.equalToSuperview()
            make.left.equalTo(icon.snp.right)
        }
        self.view = self.container
    }
    
    lazy var icon: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"icon_changeName_select")!)
        imageV.isHidden = true
        return imageV
    }()
    
    func loadData() {
        let string = self.string
        if string[string.startIndex] == "&" {
            self.isEnabled = false
            self.string = string.substring(fromIndex: 1)
        }else {
            self.string = string
            self.isEnabled = true
            self.view?.setHover()
        }
        
    }
    
    lazy var container: MSView = {
        let view = MSView.init()
        view.tap = { sview in
            if self.saction != nil {
                self.saction!(self)
            }
        }
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.clear.cgColor
        return view
    }()
    
    lazy var sTitle: NSLabel = {
        let lab = NSLabel.label(title: self.string, textColor: color_gray(40), font: NSFont.systemFont(ofSize: 14))
        lab.sizeToFit()
        return lab
    }()
    
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
